
import { Injectable } from '@angular/core';
import { CanDeactivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';

import { EditorComponent } from '../components/editor/editor';
import { DialogService } from './DialogService';
import { FileService } from './FileService';


@Injectable()
export class EditorDeactivateGuardService implements CanDeactivate<EditorComponent> {

    constructor (
        private dialogService: DialogService,
        private fileService: FileService
    ) {}

    canDeactivate(component: EditorComponent, route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {

        // Allow synchronous navigation (`true`) if no crisis or the crisis is unchanged
        if (component.untouched || !component.content) {
            return of(true);
        }
        // Otherwise ask the user with the dialog service and return its
        // promise which resolves to true or false when the user decides
        return this.dialogService.confirm({message: '放弃保存?'});
    }
}
